package com.room.search.service;

import com.room.search.domain.RoomUserPermission;
import com.room.search.repository.RoomUserPermissionRepository;
import com.room.search.service.dto.RoomUserPermissionDTO;
import com.room.search.service.mapper.RoomUserPermissionMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link RoomUserPermission}.
 */
@Service
@Transactional
public class RoomUserPermissionService {

    private final Logger log = LoggerFactory.getLogger(RoomUserPermissionService.class);

    private final RoomUserPermissionRepository roomUserPermissionRepository;

    private final RoomUserPermissionMapper roomUserPermissionMapper;

    public RoomUserPermissionService(
        RoomUserPermissionRepository roomUserPermissionRepository,
        RoomUserPermissionMapper roomUserPermissionMapper
    ) {
        this.roomUserPermissionRepository = roomUserPermissionRepository;
        this.roomUserPermissionMapper = roomUserPermissionMapper;
    }

    /**
     * Save a roomUserPermission.
     *
     * @param roomUserPermissionDTO the entity to save.
     * @return the persisted entity.
     */
    public RoomUserPermissionDTO save(RoomUserPermissionDTO roomUserPermissionDTO) {
        log.debug("Request to save RoomUserPermission : {}", roomUserPermissionDTO);
        RoomUserPermission roomUserPermission = roomUserPermissionMapper.toEntity(roomUserPermissionDTO);
        roomUserPermission = roomUserPermissionRepository.save(roomUserPermission);
        return roomUserPermissionMapper.toDto(roomUserPermission);
    }

    /**
     * Partially update a roomUserPermission.
     *
     * @param roomUserPermissionDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<RoomUserPermissionDTO> partialUpdate(RoomUserPermissionDTO roomUserPermissionDTO) {
        log.debug("Request to partially update RoomUserPermission : {}", roomUserPermissionDTO);

        return roomUserPermissionRepository
            .findById(roomUserPermissionDTO.getId())
            .map(existingRoomUserPermission -> {
                roomUserPermissionMapper.partialUpdate(existingRoomUserPermission, roomUserPermissionDTO);

                return existingRoomUserPermission;
            })
            .map(roomUserPermissionRepository::save)
            .map(roomUserPermissionMapper::toDto);
    }

    /**
     * Get all the roomUserPermissions.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<RoomUserPermissionDTO> findAll() {
        log.debug("Request to get all RoomUserPermissions");
        return roomUserPermissionRepository
            .findAll()
            .stream()
            .map(roomUserPermissionMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one roomUserPermission by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<RoomUserPermissionDTO> findOne(Long id) {
        log.debug("Request to get RoomUserPermission : {}", id);
        return roomUserPermissionRepository.findById(id).map(roomUserPermissionMapper::toDto);
    }

    /**
     * Delete the roomUserPermission by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete RoomUserPermission : {}", id);
        roomUserPermissionRepository.deleteById(id);
    }
}
